//
//  LyVistorView.swift
//  LySwift微博
//
//  Created by 贠明东 on 15/11/23.
//  Copyright © 2015年 itcCZ. All rights reserved.
//

import UIKit

//协议
protocol LyVistorViewDelegate: NSObjectProtocol
{
    // 默认是必须实现的方法
    // 登录按钮点击
    func vistorViewLoginClick()
    
//    func vistorViewRegisterClick()
}

///访客视图
class LyVistorView: UIView {
    
    // MARK: - 属性
    var isPause = false
    
    /// 代理
    weak var delegate: LyVistorViewDelegate?

    // swift默认所有的view都能通过xib/storyboard 加载
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //通过代码创建的
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //设置背景颜色(当三种颜色一样的时候可以使用【UIColor(white: ？, alpha: ？)】)
        backgroundColor = UIColor(white: 236 / 255.0, alpha: 1)
        
        prepareUI()
    }
    
    // MARK: -按钮点击事件
    func loginClick()
    {
        //通知代理
        delegate?.vistorViewLoginClick()
    }
    
    //MARK: - 转轮动画
    func rotationAnimation()
    {
        //核心动画,旋转(keyPath,网页笔记上面有需要查询说明文档)
        let rotation =  CABasicAnimation(keyPath: "transform.rotation")
        
        //设置动画参数
        //旋转360°
        rotation.toValue = 2*M_PI
        //转速(时间：秒)
        rotation.duration = 11
        //重复的次数(无限大，无数次)
        rotation.repeatCount = MAXFLOAT
        
        //当跳转了页面，系统会默认动画完成并关闭动画，所以要false
        rotation.removedOnCompletion = false
        
        //开始核心动画
        iconView.layer.addAnimation(rotation, forKey: "rotation11")
    }
    
    // MARK: - 核心动画开始和暂停(比较麻烦，只需要理解，直接拷贝网页笔记)
    /// 暂停旋转
    func pauseAnimation() {
        // 记录暂停时间
        let pauseTime = iconView.layer.convertTime(CACurrentMediaTime(), fromLayer: nil)
        
        // 设置动画速度为0
        iconView.layer.speed = 0
        
        // 设置动画偏移时间
        iconView.layer.timeOffset = pauseTime
        
        isPause = true
    }
    
    /// 恢复旋转
    func resumeAnimation() {
        if !isPause {
            return
        }
        
        // 获取暂停时间
        let pauseTime = iconView.layer.timeOffset
        
        // 设置动画速度为1
        iconView.layer.speed = 1
        
        iconView.layer.timeOffset = 0
        
        iconView.layer.beginTime = 0
        
        let timeSincePause = iconView.layer.convertTime(CACurrentMediaTime(), fromLayer: nil) - pauseTime
        
        iconView.layer.beginTime = timeSincePause
        
        isPause = false
    }
    
    //MARK: - 设置访客视图的内容
    func setupVistorView(imageName: String , message: String)
    {
        //图片,替换转轮的图片
        iconView.image = UIImage(named: imageName)
        
        //Lable文字
        messageLable.text = message
        //自动适应大小
        messageLable.sizeToFit()
        
        //隐藏小房子
        homeView.hidden = true
        
        //隐藏除了主页的遮罩图(方法一)
        coverView.hidden = true
        
        // 父控件把子控件 coverView移动最后面(方法二)
        //        sendSubviewToBack(coverView)(方法二)
        
        // 父控件把子控件coverView移动最前面
        //        bringSubviewToFront(coverView)
    }
    
    
    //:MARK: -准备UI
    private func prepareUI() {
        
        //1.添加子控件
        addSubview(iconView)
        //添加遮罩
        addSubview(coverView)
        addSubview(homeView)
        addSubview(messageLable)
        addSubview(registerButton)
        addSubview(loginButton)
        
        //2.添加约束 aotulayout
        iconView.translatesAutoresizingMaskIntoConstraints = false
        coverView.translatesAutoresizingMaskIntoConstraints = false
        homeView.translatesAutoresizingMaskIntoConstraints = false
        messageLable.translatesAutoresizingMaskIntoConstraints = false
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        
        /**
        *  创建约束对象
        *
        *  item：要添加约束的View
        *  attribute：设置的约束
        *  relatedBy：Equal(相等)
        *  toltem：参照的View
        */
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        ///iconView CenterY与父控件的Center重合
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))
        
        //小房子
        //X
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        //Y
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        //消息Lable
        //X
        addConstraint(NSLayoutConstraint(item: messageLable, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        //Y
        addConstraint(NSLayoutConstraint(item: messageLable, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        //宽度约束
        addConstraint(NSLayoutConstraint(item: messageLable, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 226))
        
        //注册按钮
        //左边和Lable对齐
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: messageLable, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        
        //顶部距离Lable底部
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLable, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        //宽度约束
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 90))
        
        //高
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        //登录按钮
        //右边和Lable对齐
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: messageLable, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        
        //顶部距离Lable底部
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLable, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        //宽度约束
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 90))
        
        //高
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        //遮罩约束
        //左边
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        
        //上
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        
        //右
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        
        //下
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
    }

    //MARK: -懒加载
    ///转轮
    lazy var iconView:UIImageView = UIImageView (image: UIImage(named: "visitordiscover_feed_image_smallicon") )
    
    ///小房子
    lazy var homeView:UIImageView = UIImageView (image: UIImage(named: "visitordiscover_feed_image_house") )

    ///消息Lable
    lazy var messageLable: UILabel = {
        let lable = UILabel()
        
        //设置Lable标签条颜色
        lable.textColor = UIColor.darkGrayColor()
        
        //设置Lable文本
        lable.text = "赶紧登录(注册),即可与女神一起撸"
        lable.numberOfLines = 0
        
        return lable
    }()
    
    ///注册按钮
     private lazy var registerButton: UIButton = {
    let button = UIButton()
        
        //设置按钮的背景图片
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        //设置文本
        button.setTitle("注册", forState: UIControlState.Normal)
        //设置文本文字颜色
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        //设置文本大小
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        //根据内容适应大小
        button.sizeToFit()
        
        return button
    }()
    
    ///登录按钮
   private lazy var loginButton:UIButton = {
    let button = UIButton()
    
    //设置按钮的背景图片
    button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)

    //设置按钮文本内容
    button.setTitle("登录", forState: UIControlState.Normal)
    
    //设置按钮文本颜色
    button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
    
    //设置文本大小
    button.titleLabel?.font = UIFont.systemFontOfSize(14)
    
    //根据内容适应大小
    button.sizeToFit()
    
    //添加点击事件
    button.addTarget(self, action: "loginClick", forControlEvents: UIControlEvents.TouchUpInside)
    
    return button
    }()
    
    ///遮罩视图  [ 要告知类型(:UIImageView)不然系统会报错 ]
    private lazy var coverView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
 }



